package com.mm.dss.demo;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.google.gson.Gson;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.adapter.Myadapter;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.tools.BottomClick;
import com.mm.dss.demo.tools.ClientUploadUtils;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.Utils;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.serialization.SoapObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;

/*
 * 信息采集
 * */
public class XxcjActivity extends BaseActivity {
    RelativeLayout layout_back;
    TextView txt_title;//
    EditText title_jiance;//监测点
    private QMUIListPopup namelistpopup;//位置名称弹出框
    private int IMAGE_PICKER = 0;
    private Button btn;//提交按钮
    ArrayList<ImageItem> images;
    private EditText edit_xxxx;//巡查信息
    private Dialog mWeiboDialog;
    TextView zhang;//几张图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xxcj);
        title_jiance = (EditText) findViewById(R.id.title_jiance);
        txt_title = (TextView) findViewById(R.id.txt_title);
        layout_back = (RelativeLayout) findViewById(R.id.layout_back);
        btn = (Button) findViewById(R.id.btn);
        edit_xxxx = (EditText) findViewById(R.id.edit_xxxx);
        zhang = (TextView) findViewById(R.id.zhang);
        txt_title.setText("信息采集");
        layout_back.setVisibility(View.VISIBLE);
    }

    public void onclick(final View view) {
        switch (view.getId()) {
            case R.id.btn:
                if (BottomClick.Click()) {
                    if (title_jiance.getText().toString().equals("")) {
                        Toast.makeText(XxcjActivity.this, "请输入监测点", Toast.LENGTH_LONG).show();
                    } else if (edit_xxxx.getText().toString().equals("")) {
                        Toast.makeText(XxcjActivity.this, "请输入巡查信息", Toast.LENGTH_LONG).show();

                    } else {
                        mWeiboDialog = WeiboDialogUtils.
                                createLoadingDialog(XxcjActivity.this,
                                        "加载中...");
                        Log.i("上报正在", "正在执行点击事件");
                        List<String> imglist = new ArrayList<>();
                        if (images != null && images.size() > 0) {
                            for (int i = 0; i < images.size(); i++) {
                                // imglist.add(new File(images.get(i).path));
                                Bitmap bitmap = Utils.compressBitmap(images.get(i).path, 600, 1024, 30);
                                imglist.add(Utils.bitmapToBase64(bitmap));
                                Log.i("上报的图片", "" + images.get(i).path);
                                Log.i("上报的图片=", "" + imglist.get(i));
                            }
                        }
                        Map<String, Object> map = new HashMap<>();
                        map.put("ID", SharedPreferencedUtils.getString(XxcjActivity.this, "username"));
                        map.put("XCRY", SharedPreferencedUtils.getString(XxcjActivity.this, "username"));
                        map.put("KSMC", title_jiance.getText().toString());
                        map.put("CJWT", edit_xxxx.getText().toString());
                        map.put("XCRQ", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())));
                        map.put("IMG", imglist);
                        // map.put("rusername",SharedPreferencedUtils.getString(XxcjActqivity.this,"realname"));
                        Gson gson = new Gson();
                        String jsonText = gson.toJson(map);
                        Log.i("json转换的数据", jsonText);
                        Map<String, String> jsonmap = new HashMap<>();
                        jsonmap.put("jsonText", jsonText);
                        WebServiceUtils.callWebService(Constances.webhosturl, "N07_MessUp", jsonmap, new WebServiceUtils.WebServiceCallBack() {
                            @Override
                            public void callBack(SoapObject result) {
                                WeiboDialogUtils.closeDialog(mWeiboDialog);
//                            Log.i("上报成功1", result.getProperty(0).toString());
                                Toast.makeText(XxcjActivity.this, "上报成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });
                    }
                }
                break;
            case R.id.layout_back:
                finish();
                break;
            case R.id.btnimg:
                //图片选择
                Intent intent = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent, IMAGE_PICKER);
                break;
        }
    }

    class NameAdapter extends BaseAdapter {
        //位置名称
        List<String> data;

        private NameAdapter(List<String> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(XxcjActivity.this).inflate(R.layout.item_spinner_view, null);
            TextView txt = (TextView) convertView.findViewById(R.id.txt);
            txt.setText(data.get(position));
            return convertView;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == IMAGE_PICKER) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                Log.i("图片选择", images.get(0).path + "刘习亭");
                zhang.setText(images.size() + "张");
                // List<String> list=new ArrayList<>();
                // xiala.setText((imgselectList.size())+"张");
        /*        for (int i=0;i<images.size();i++){
                    list.add(images.get(i).path);
                }
                Myadapter adapter = new Myadapter(list,XxcjActivity.this);
                gridView.setAdapter(adapter);*/
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }


}
